/*
 *  ------Waspmote starting program------
 *
 *  Explanation: Send basic parameters through the corresponding 
 *  XBee module.
 *  For other communication modules please use on line code generator.
 *
 *  Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:         Yuri Carmona
 */

// Include libraries
#include <WaspXBeeDM.h>
#include <WaspFrame.h>


// Declare global variables
packetXBee* packet;


void setup()
{
  /////////////////////////////////
  // 1. set up the XBee module
  /////////////////////////////////
  xbeeDM.ON();
  
  /////////////////////////////////
  // 2. Set up RTC and ACC
  /////////////////////////////////
  delay(500);
  RTC.ON();
  ACC.ON();   

  /////////////////////////////////
  // 3. LEDs management 
  /////////////////////////////////  
  Utils.setLED(LED0, LED_ON);
  Utils.setLED(LED1, LED_ON);
  delay(2000);
  Utils.setLED(LED0, LED_OFF);
  Utils.setLED(LED1, LED_OFF);
  for (int i=0;i<24;i++)
  {
    Utils.blinkLEDs(125);
  }

}


void loop()
{
  ////////////////////////////////////////////////
  // 4. Message composition
  ////////////////////////////////////////////////
  
  // 4.1 Create new frame (No mote id)
  frame.createFrame(ASCII,"");  
  
  // 4.2 Add frame fields
  frame.addSensor(SENSOR_ACC, ACC.getX(), ACC.getY(), ACC.getZ() );
  frame.addSensor(SENSOR_IN_TEMP, RTC.getTemperature());
  frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());
  
  // 4.3 Print frame
  // Example:<=>#35690399##5#ACC:-47;-26;1000#IN_TEMP:26.25#BAT:59#
  frame.showFrame();
  
  ////////////////////////////////////////////////
  // 5. Send the packet
  ////////////////////////////////////////////////
  
  // 5.1 set packet to send
  packet=(packetXBee*) calloc(1,sizeof(packetXBee)); // memory allocation
  packet->mode=BROADCAST; // set Unicast mode
  
  // 5.2 send the packet via the correct object depending on the protocol  
  // turn XBee on
  xbeeDM.ON();  
  // sets Destination parameters
  xbeeDM.setDestinationParams(packet, "000000000000FFFF", frame.buffer, frame.length); 
  // send data
  xbeeDM.sendXBee(packet);
    
  // check TX flag
  if( xbeeDM.error_TX == 0 )
  {
    USB.println(F("sending ok"));
  }
  else 
  {
    USB.println(F("sending error"));
  }    
   
  // 5.3 free memory
  free(packet);
  packet = NULL;
  
  delay(1000);
}
